package com.dhh.sky.widget;

import com.dhh.sky.R;
import com.dhh.sky.model.Operation;
import com.dhh.sky.util.Constant;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.LinearLayout.LayoutParams;

public class TransitionFrame extends Fragment {
    private ListView listView;
    private BaseAdapter adapter;
    private String tab;
    
    public static TransitionFrame newInstance(String tab) {
    	TransitionFrame fragment = new TransitionFrame();
    	fragment.setTab(tab);
        return fragment;
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    	listView = new ListView(getActivity());
    	listView.setTextFilterEnabled(true);
		
		listView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		if(Constant.TRANSFER_DOWNLOAD_COMPLETED.equalsIgnoreCase(tab)){
			adapter = new TransitionTabDownloadAdapter(this.getActivity());					
		}else if(Constant.TRANSFER_DOWNLOAD_UNCOMPLETED.equalsIgnoreCase(tab)){
			adapter = new TransitionTabOperationAdapter(this.getActivity(),Operation.OP_DOWNLOAD,Constant.TRANSFER_UNCOMPLETED);
		}else if(Constant.TRANSFER_UPLOAD_COMPLETED.equalsIgnoreCase(tab)){
			adapter = new TransitionTabOperationAdapter(this.getActivity(),Operation.OP_UPLOAD,Constant.TRANSFER_COMPLETED);
			listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
	            public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
	            	View toolbar = view.findViewById(R.id.toolbar);
					ListItemAnimation expandAni = new ListItemAnimation(toolbar, 500);
					toolbar.startAnimation(expandAni);
	            }
	        });
		}else if(Constant.TRANSFER_UPLOAD_UNCOMPLETED.equalsIgnoreCase(tab)){
			adapter = new TransitionTabOperationAdapter(this.getActivity(),Operation.OP_UPLOAD,Constant.TRANSFER_UNCOMPLETED);
		}
		
    	listView.setAdapter(adapter);
		
		LinearLayout layout = new LinearLayout(getActivity());
        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        layout.addView(listView);
        
        return layout;
    }
    
    public void setTab(String tab){
    	this.tab = tab;
    }

}
